<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i>
          <span style="font-size:26px">{{ this.name }}</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <br />
    <br />
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          icon="el-icon-check"
          class="handle-del mr10"
          @click="confirmAllation"
          >批量确认</el-button
        >
        <el-select
          v-model="query.address"
          placeholder="不限"
          class="handle-select mr10"
        >
          <el-option key="1" label="不限" value="不限"></el-option>
          <el-option key="2" label="已确认" value="已确认"></el-option>
          <el-option key="3" label="未确认" value="未确认"></el-option>
        </el-select>
        <el-input
          v-model="username"
          placeholder="用户名"
          class="handle-input mr10"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"
          >搜索</el-button
        >
        <el-button
          type="success"
          class="el-icon-document"
          @click="downloadExcel"
          >导出文件</el-button
        >
        <el-button type="danger" class="el-icon-close" @click="deleteaction"
          >结束报名</el-button
        >
      </div>

      <el-table
        :data="tableData"
        border
        class="table"
        ref="filterTable"
        :row-key="row => row.routerid"
        v-loading="loadingTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="center"></el-table-column>
        <!--        <el-table-column prop="id" label="学号" align="center"></el-table-column>-->

        <el-table-column
          prop="code"
          label="学号"
          align="center"
          sortable
          column-key="code"
        >
        </el-table-column>

        <el-table-column
          prop="name"
          label="姓名"
          width="70px"
          align="center"
        ></el-table-column>

        <el-table-column label="学院">
          <template slot-scope="scope"
            >{{ scope.row.college }}<br />{{ scope.row.className }}</template
          >
        </el-table-column>

        <el-table-column prop="createDate" label="报名时间" align="center">
          <template slot-scope="scope">{{
            transTime(scope.row.createDate)
          }}</template>
        </el-table-column>

        <el-table-column label="性别" width="70px" align="center">
          <template slot-scope="scope">{{
            scope.row.gender === 0 ? "男" : "女"
          }}</template>
        </el-table-column>

        <el-table-column label="邮箱/电话" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.mail }}</span>
            <el-divider></el-divider>
            <span>{{ scope.row.tel }}</span>
          </template>
          <!--          <el-table-column prop="mail"></el-table-column>-->
          <!--          <el-table-column prop="tel"></el-table-column>-->
        </el-table-column>

        <el-table-column label="状态" width="100px" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.confirm === 1 ? 'info' : ''" effect="dark">
              {{ scope.row.confirm === 1 ? "已确认" : "未确认" }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <!--            scope.row.confirm===1?true:false-->
            <el-button
              type="success"
              icon="el-icon-check"
              round
              :disabled="scope.row.confirm === 1 ? true : false"
              @click="checkuser(scope.row.id)"
              >{{ scope.row.confirm === 1 ? "已确认" : "确认" }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="this.routerpageNum"
          :page-size="this.routerpageSize"
          :total="this.total"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import HotEvent from "../../../api/HotEvent";
export default {
  name: "basetable",
  data() {
    return {
      confirm: null,
      username: "",
      name: "",
      from: {},
      flag1: false,
      query: {
        address: null,
        name: "",
        pageIndex: 1,
        pageSize: 10
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      pageTotal: 0,
      form: {},
      idx: -1,
      id: -1,
      routerid: 1,
      routerpageNum: 1,
      routerpageSize: 10,
      list: {},
      loadingTable: false,
      total: 0
    };
  },
  async mounted() {
    const num = JSON.parse(localStorage.getItem("params"));
    this.routerid = num.id;
    this.routerpageNum = 1;
    this.routerpageSize = 10;
    this.name = num.name;
    const list = await HotEvent.enrollUsers({
      aid: this.routerid,
      pageNum: this.routerpageNum,
      pageSize: this.routerpageSize
    });
    this.total = list.data.total;
    this.tableData = list.data.list;
    console.log(list.data);
    if (list.data.total === 0) {
      this.$message({
        message: "还没有人报名哦~~~",
        type: "warning"
      });
    }
  },
  methods: {
    // 取消活动
    async deleteaction() {
      const data = await HotEvent.deleteaction({
        aid: this.routerid
      });
      console.log("结束报名数据：");
      console.log(data);
      this.$message({
        message: data.msg,
        type: "error"
      });
    },
    // 导出文件，daochuexcel文件
    async downloadExcel() {
      const data = await HotEvent.downloadExcel({
        aid: this.routerid,
        name: this.name
      });
    },
    // 时间戳转换
    transTime(oldtime) {
      const time = new Date(oldtime);
      let y = time.getFullYear();
      let MM = time.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let d = time.getDate();
      d = d < 10 ? "0" + d : d;
      const newtime = y + "年" + MM + "月" + d + "日";
      return newtime;
    },
    // 确认用户提交到接口
    async checkuser(uid) {
      const data = await HotEvent.checkconfirm({
        uid: uid,
        aid: this.routerid
      });
      if (data.code === 0) {
        setTimeout(() => {
          this.$message({
            type: "success",
            message: data.msg
          });
        });
        // 进行tableData的更新
        const list = await HotEvent.enrollUsers({
          aid: this.routerid,
          pageNum: this.routerpageNum,
          pageSize: this.routerpageSize
        });
        this.tableData = list.data.list;
      }
      console.log(data.code);
    },
    // 触发搜索按钮
    async handleSearch() {
      const { pageSize, pageNum, id: aid } = JSON.parse(
        localStorage.getItem("params")
      );
      let data = null;
      if (this.query.address === "不限" || this.query.address === null) {
        data = await HotEvent.searchEnrollUser({
          pageNum: pageNum,
          pageSize: pageSize,
          info: this.username,
          aid: aid
        });
      } else {
        data = await HotEvent.searchEnrollUser({
          pageNum: pageNum,
          pageSize: pageSize,
          info: this.username,
          aid: aid,
          confirm: this.query.address === "已确认" ? 1 : 0
        });
      }

      this.tableData = data.data.list;
      if (data.data.list.length === 0) {
        setTimeout(() => {
          this.$message({
            type: "error",
            message: "暂时还没有此用户的信息哦~~~"
          });
        }, 2000);
      }
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 批量确认
    async confirmAllation() {
      const length = this.multipleSelection.length;
      this.delList = this.delList.concat(this.multipleSelection);
      let list = [];
      for (let i = 0; i < length; i++) {
        list[i] = this.multipleSelection[i].id;
      }
      const data = await HotEvent.confirmAllation({
        aid: this.routerid,
        uids: list
      });
      if (data.code === 0) {
        this.$message({
          type: "success",
          message: data.msg
        });
      } else {
        this.$message({
          type: "error",
          message: data.msg
        });
      }
      console.log("批量确认到的数据是：");
      console.log(data);

      this.multipleSelection = [];
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
